package com.example.springboottools.utils.staples.test;

import cn.hutool.http.webservice.SoapClient;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;

import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SendPoheaderWebserviceUtils {
    public static void main(String[] args) {
        try {
            sendPoDataTest();
        } catch (SOAPException e) {
            throw new RuntimeException(e);
        }
    }

    //推送采购订单
    public static void sendPoDataTest() throws SOAPException {
        // 创建SoapClient对象
        SoapClient soapClient = SoapClient.create("http://10.10.5.223:9997/ServiceCenter-VPServer/ws/receiveSapPoData?wsdl")
                // SoapClient soapClient = SoapClient.create("http://localhost:8081/ServiceCenter_VPServer/ws/receiveSapPoData?wsdl")
                .setMethod("ser:receiveSapPoData","http://service.webservice.staples.com/");
        SOAPElement params = soapClient.getMethodEle().addChildElement("params");
        // String ZVP_POHEADER="{BEDAT=2023-08-16, FRGKE=, EBELN=4500815106, EKORG=SH01, KDATE=2023-09-05, UNSEZ=, ZSTATUS=, NAME1=迈库司（ 上海 ）商贸有限公司(STB）, LIFNR=0000100303, TEL_VENDER=021-31338105 18017128209, EKGRP=113, ERNAM=黄彬, BSART=ZNB, BATXT=经销正常商品采购订单, FILEPATH=, PTELF=, REGION=, CITY1=, CITY2=, DELADDRESS=上海 长宁区通协路269号5号楼3楼4楼, HEADERTEXT=, STATUSFLAG=I, PROCESSDATE=2023-08-16, PROCESSTIME=11:32:16, PROCESSSTATUS=Y, VERSION=00001, NAME2=, TEL_NUMBER=021-52609020, VBELN=, EMERGENCY=, RE_NAME1=, RE_TEL_NUMBER=, RE_MOB_NUMBER=, PO_TEXT=, DELFLG=, HKUNNR=, HKUNNR_NAME=, SUM_BRTWR=3097.20, SLFDT=2023-08-16, PFLAG=, KUNNR=, KUNNR_NAME=, BSTKD=, DELIV_URL=, LABEL_URL=, LIFNR_UNION=, LIFNR_SEC=, ORDER_DATE=, ZZDELIVERY_REQ=, PO_ELIKZ=, ERNAM_POD=, FAX_NUMBER=, TEL_EXTENS=, ERDAT_TXT=, TELFX=, ZLSCH=, ZLSCHT=, NAME1_POD=, PA_KUNNR=, TITLE_COMP=, ZZZDJCBS=, ZDEL_FLAG=, ZAPPOINT=Y, ZKUNNR_FP=, ZKUNNR_FPNAME=, UNAME_SEND=ZBACKGROUND, ERDAT=00000000, ERZET=000000, CREATE_DATE_SO=00000000, CREATE_TIME_SO=000000, RETPO=, ZSOSALER=, ZOMSREGIO=, ZOMSCITY=, ERNAM_SO=}";
        String ZVP_POHEADER = "{BEDAT=2025-06-23, FRGKE=, EBELN=4500894944, EKORG=SH01, KDATE=2025-05-30, UNSEZ=, ZSTATUS=, NAME1=上海卓润资产管理有限公司(STB), LIFNR=0000700017, TEL_VENDER=53510053 13774389278, EKGRP=101, ERNAM=蒲海波, BSART=ZNFS, BATXT=供应商直送采购, FILEPATH=, PTELF=, REGION=040, CITY1=通辽, CITY2=, DELADDRESS=内蒙古通辽市科尔沁区西顺路156号 科尔沁供电公司, HEADERTEXT=, STATUSFLAG=U, PROCESSDATE=2025-07-01, PROCESSTIME=10:10:07, PROCESSSTATUS=Y, VERSION=00059, NAME2=常建民, TEL_NUMBER=22222 18504758892, VBELN=0040338111, EMERGENCY=, RE_NAME1=常建民, RE_TEL_NUMBER=22222, RE_MOB_NUMBER=18504758892, PO_TEXT=22222, DELFLG=, HKUNNR=9000000416, HKUNNR_NAME=国家电网, SUM_BRTWR=39634.00, SLFDT=2025-05-30, PFLAG=, KUNNR=1000423958, KUNNR_NAME=国网内蒙古东部电力有限公司-内蒙古通辽市科尔沁区, BSTKD=ASDAS, DELIV_URL=, LABEL_URL=, LIFNR_UNION=, LIFNR_SEC=, ORDER_DATE=, ZZDELIVERY_REQ=只要发顺丰配送要求111, PO_ELIKZ=, ERNAM_POD=, FAX_NUMBER=, TEL_EXTENS=, ERDAT_TXT=, TELFX=, ZLSCH=, ZLSCHT=, NAME1_POD=常民, PA_KUNNR=, TITLE_COMP=, ZZZDJCBS=, ZDEL_FLAG=, ZAPPOINT=Y, ZKUNNR_FP=, ZKUNNR_FPNAME=, UNAME_SEND=SH102909, ERDAT=20250529, ERZET=162057, CREATE_DATE_SO=00000000, CREATE_TIME_SO=000000, RETPO=, ZSOSALER=, ZOMSREGIO=11, ZOMSCITY=902, ERNAM_SO=SH103500, ZPODREQ=10, PRINT_FLAG=, ZOMSCITY1=, ZPRICE_DISPLAY=, ZSTBPRINT_FLAG=S0, KUNWE=2002673427, ZNUM_ERP=, ZBFLIFNR=, ZBACKFLOWFLAG=, ZDH_TIME_YQ=, ZDH_TIME=, ZWORKDAYS=, PURCHASE_TEL_NUMBER=1111111, PURCHASE_MOB_NUMBER=18504758891, ZTERM=T015, ZZTELOPR=SH103500, ZZTELOPR_NAME=蒲海波, BSTKD2=}";
        //String zvp_poitem="[{EBELN=4500815106, EBELP=00010, MENGE=12.000, PEINH=1, NETPR=1.04, MEINS=EA, NETWR=12.45, BRTWR=13.20, LOEKZ=, RETPO=, EMATN=000000001100000017, MATKL=D10201020, WERKS=D001, BPRME=EA, LGORT=B001, ELIKZ=, ZBASICQTY=12.000, MATNR=000000001100000017, WGBEZ=手写标签, UNION_FLAG=, EKNAM=耿龙琴, ZZUINT_DES=包, LGOBE=办公区, MAKTX=国产   口取纸 26X21MM（80张/包） 红色, ZZSHOW_NAME=国产   口取纸 26X21MM（80张/包） 红色, BUTXT=史泰博（上海）有限公司, EKOTX=史泰博（上海）有限公司, BISMT=1113033, BRAND_ID=1301, NAMEV=迈库司（ 上海 ）商贸有限公司(STB）, ORT01=上海市闵行区吴中路686弄2号E座1505室, ZZSTOCK=聂荣, ZZBUYER=乐静思, WGBEZ_1=便签, WGBEZ_2=标签, WGBEZ_3=手写标签, TELF1=021-31338105, APLFZ=20, BUDAT=0000-00-00, SLFDT=2023-08-16, BEZEI=ZOTH, ZNETPR=1.10, EAN11=1000001130330, ITEMTEXT=, IDNLF=, VERSION=00001, XSDJ=0.00, XSZJ=0.00, PFLAG=, JHTS=, VDATU_TXT=, BANFN=, BNFPO=00000, POSNN_O=, VBELN_O=, EBELN_TEXT=, ZPRFAR=0.00 }, {EBELN=4500815106, EBELP=00020, MENGE=12.000, PEINH=1, NETPR=31.86, MEINS=EA, NETWR=382.30, BRTWR=432.00, LOEKZ=, RETPO=, EMATN=000000001100000046, MATKL=F30001050, WERKS=D001, BPRME=EA, LGORT=B001, ELIKZ=, ZBASICQTY=12.000, MATNR=000000001100000046, WGBEZ=0.6mm开门柜, UNION_FLAG=, EKNAM=耿龙琴, ZZUINT_DES=组, LGOBE=办公区, MAKTX=集大 JD UGY-335 九门更衣柜 W900*D500*H1800 灰白色, ZZSHOW_NAME=集大 JD UGY-335 九门更衣柜 W900*D500*H1800 灰白色 仅限上海, BUTXT=史泰博（上海）有限公司, EKOTX=史泰博（上海）有限公司, BISMT=1117341, BRAND_ID=1390, NAMEV=迈库司（ 上海 ）商贸有限公司(STB）, ORT01=上海市闵行区吴中路686弄2号E座1505室, ZZSTOCK=陶丽娜, ZZBUYER=王菲菲, WGBEZ_1=办公家具, WGBEZ_2=办公柜/文件柜, WGBEZ_3=开门柜, TELF1=021-31338105, APLFZ=7, BUDAT=0000-00-00, SLFDT=2023-08-16, BEZEI=ZDSH, ZNETPR=10.00, EAN11=1000001173412, ITEMTEXT=, IDNLF=, VERSION=00001, XSDJ=0.00, XSZJ=0.00, PFLAG=, JHTS=, VDATU_TXT=, BANFN=, BNFPO=00000, POSNN_O=, VBELN_O=, EBELN_TEXT=, ZPRFAR=0.00 }, {EBELN=4500815106, EBELP=00030, MENGE=12.000, PEINH=1, NETPR=71.68, MEINS=EA, NETWR=860.18, BRTWR=972.00, LOEKZ=, RETPO=, EMATN=000000001100000047, MATKL=B10103000, WERKS=D001, BPRME=EA, LGORT=B001, ELIKZ=, ZBASICQTY=12.000, MATNR=000000001100000047, WGBEZ=打印机墨盒, UNION_FLAG=, EKNAM=耿龙琴, ZZUINT_DES=盒, LGOBE=办公区, MAKTX=惠普 8.83586E+11 打印机墨盒 680ml 青色, ZZSHOW_NAME=惠普 8.83586E+11 打印机墨盒 680ml 青色 (适用  HP5100、680ML), BUTXT=史泰博（上海）有限公司, EKOTX=史泰博（上海）有限公司, BISMT=1117353, BRAND_ID=1379, NAMEV=迈库司（ 上海 ）商贸有限公司(STB）, ORT01=上海市闵行区吴中路686弄2号E座1505室, ZZSTOCK=王润华, ZZBUYER=刘林, WGBEZ_1=打印/传真机耗材, WGBEZ_2=打印机墨盒, WGBEZ_3=打印机墨盒, TELF1=021-31338105, APLFZ=0, BUDAT=0000-00-00, SLFDT=2023-08-16, BEZEI=ZOTH, ZNETPR=0.00, EAN11=1000001173535, ITEMTEXT=, IDNLF=, VERSION=00001, XSDJ=0.00, XSZJ=0.00, PFLAG=, JHTS=, VDATU_TXT=, BANFN=, BNFPO=00000, POSNN_O=, VBELN_O=, EBELN_TEXT=, ZPRFAR=0.00 }, {EBELN=4500815106, EBELP=00040, MENGE=12.000, PEINH=1, NETPR=123.89, MEINS=EA, NETWR=1486.73, BRTWR=1680.00, LOEKZ=, RETPO=, EMATN=000000001100000048, MATKL=D10000010, WERKS=D001, BPRME=EA, LGORT=B001, ELIKZ=, ZBASICQTY=12.000, MATNR=000000001100000048, WGBEZ=钢笔, UNION_FLAG=, EKNAM=耿龙琴, ZZUINT_DES=支, LGOBE=办公区, MAKTX=美的 SJ1702HM 1.7L电水壶 210*200*238, ZZSHOW_NAME=英雄 329 钢笔 特细, BUTXT=史泰博（上海）有限公司, EKOTX=史泰博（上海）有限公司, BISMT=1019836, BRAND_ID=2074, NAMEV=迈库司（ 上海 ）商贸有限公司(STB）, ORT01=上海市闵行区吴中路686弄2号E座1505室, ZZSTOCK=王圣洁, ZZBUYER=叶琼, WGBEZ_1=书写修正用品, WGBEZ_2=墨水书写用具, WGBEZ_3=钢笔, TELF1=021-31338105, APLFZ=0, BUDAT=0000-00-00, SLFDT=2023-08-16, BEZEI=NORM, ZNETPR=0.00, EAN11=6935965502761, ITEMTEXT=, IDNLF=, VERSION=00001, XSDJ=0.00, XSZJ=0.00, PFLAG=, JHTS=, VDATU_TXT=, BANFN=, BNFPO=00000, POSNN_O=, VBELN_O=, EBELN_TEXT=, ZPRFAR=0.00 }]";
        String zvp_poitem = "[{EBELN=4500894944, EBELP=00010, MENGE=40.000, PEINH=1, NETPR=876.86, MEINS=EA, NETWR=35074.34, BRTWR=39634.00, LOEKZ=, RETPO=, EMATN=000000007700028168, MATKL=C00604010, WERKS=D001, BPRME=EA, LGORT=, ELIKZ=, ZBASICQTY=40.000, MATNR=000000007700028168, WGBEZ=无线键盘, UNION_FLAG=, EKNAM=张焕彩, ZZUINT_DES=把, LGOBE=, MAKTX=罗技 MX MECHANICAL 商务低噪无线双模矮轴机械键盘 智能键盘 110, ZZSHOW_NAME=罗技 MX MECHANICAL 商务低噪无线双模矮轴机械键盘 智能键盘 110键, BUTXT=国网内蒙古东部电力有限公司-内蒙古通辽市科尔沁区, EKOTX=史泰博（上海）有限公司, BISMT=, BRAND_ID=1609, NAMEV=上海卓润资产管理有限公司(STB), ORT01=上海市延安东路175号20楼, ZZSTOCK=, ZZBUYER=SH103889, WGBEZ_1=办公外设, WGBEZ_2=键盘, WGBEZ_3=无线键盘, TELF1=53510053, APLFZ=0, BUDAT=0000-00-00, SLFDT=2025-05-30, BEZEI=ZDSH, ZNETPR=0.00, EAN11=2000004140687, ITEMTEXT=, IDNLF=, VERSION=00058, XSDJ=1043.00, XSZJ=41720.00, PFLAG=, JHTS=, VDATU_TXT=, BANFN=0010360177, BNFPO=00080, POSNN_O=, VBELN_O=, EBELN_TEXT=, ZPRFAR=5.00, ZPRFAR_BACKUP=5.00, ZNUM=2000000020, ZSECPRFAR=, VBELN=0040338111, VBELP=000080 , ITEM_REMARK=测试测试测试测试测试测试测试测试1111111测试测试测试测试测试测试测试测试1111111测试测试测试测试测试测试测试测试1111111测试测试测试测试测试测试测试测试1111111测试测试测试测试测试测试测试测试1111111}]";
        Gson gson=new Gson();
        //Map<String,Object> headerMap = gson.fromJson(ZVP_POHEADER, Map.class);
        Map<String,String> headerMap = new HashMap<>();
        ZVP_POHEADER=ZVP_POHEADER.replaceAll("[{}]","");
        String[] poheaderArray = ZVP_POHEADER.split(",");
        for (String s : poheaderArray) {
            if (StringUtils.isNotBlank(s)){
                String[] split = s.split("=");
                if (split.length>1){
                    headerMap.put(split[0],split[1]);
                }else {
                    headerMap.put(split[0],"");
                }
            }

        }
        //List<Map<String, Object>> itemList = gson.fromJson(zvp_poitem, ArrayList.class);
        List<Map<String, String>> itemList =new ArrayList<>();
        zvp_poitem=zvp_poitem.replaceAll("[\\[\\]]","");
        String[] poitemArray = zvp_poitem.split("}");
        for (String s : poitemArray) {
            String s1 = s.replaceAll("\\{", "");
            String[] split = s1.split(",");
            Map<String, String> itemMap=new HashMap<>();
            for (String s2 : split) {
                if (StringUtils.isNotBlank(s2)){
                    String[] split1 = s2.split("=");
                    if (split1.length>1){
                        itemMap.put(split1[0],split1[1]);
                    }else {
                        itemMap.put(split1[0],"");
                    }
                }

            }
            itemList.add(itemMap);
        }
        SOAPElement zvp_poheader = params.addChildElement("ZVP_POHEADER");
        for (Map.Entry<String,String> entry:headerMap.entrySet()) {
            String key = entry.getKey().trim();
            String value = entry.getValue();
            zvp_poheader.addChildElement(key).setValue(value);
        }
        for (Map<String, String> objectMap : itemList) {
            SOAPElement zvp_poitem1 = params.addChildElement("ZVP_POITEM");
            for (Map.Entry<String, String> entry:objectMap.entrySet()) {
                String key=entry.getKey().trim();
                zvp_poitem1.addChildElement(key).setValue(entry.getValue());
            }
        }
        System.out.println(soapClient.send(true));
    }
    /**
     * 推送PO开票，核销单状态
     * @throws SOAPException
     */
    public static void sendPoStatusTest() throws SOAPException {
        // 创建SoapClient对象
//        SoapClient soapClient = SoapClient.create("http://localhost:8081/ServiceCenter_VPServer/ws/receiveSapPoStatus?wsdl")
//                .setMethod("ser:receiveSapPoStatus","http://service.webservice.staples.com/");
        // 创建SoapClient对象
        SoapClient soapClient = SoapClient.create("http://10.10.5.223:9999/ServiceCenter-VPServer/ws/receiveSapPoStatus?wsdl")
                .setMethod("ser:receiveSapPoStatus","http://service.webservice.staples.com/");
        SOAPElement params = soapClient.getMethodEle().addChildElement("params");
        for (int i = 0; i < 1; i++) {
            SOAPElement clearInfoList = params.addChildElement("clearInfoList");
            clearInfoList.addChildElement("EBELN").setValue("4500814997");
            clearInfoList.addChildElement("BUKRS").setValue("3071");
            clearInfoList.addChildElement("VBELN").setValue("0040308736");
            clearInfoList.addChildElement("ZHXDAT").setValue("00000000");
            clearInfoList.addChildElement("ZHXTIM").setValue("00");
            clearInfoList.addChildElement("ZHXZT").setValue("10");
            clearInfoList.addChildElement("ZSOZBJ").setValue("117");
            clearInfoList.addChildElement("ZSOZBJHK").setValue("118");
            SOAPElement invoicingInfoList = params.addChildElement("paymentInfoList");
            invoicingInfoList.addChildElement("EBELN").setValue("4500814997");
            invoicingInfoList.addChildElement("BUKRS").setValue("3071");
            invoicingInfoList.addChildElement("BELNR").setValue("996");
            invoicingInfoList.addChildElement("GJAHR").setValue("996");
            invoicingInfoList.addChildElement("SGTXT").setValue("996");
            invoicingInfoList.addChildElement("ZPOZBJKK").setValue("112");
            invoicingInfoList.addChildElement("ZPOZBJFK").setValue("113");
            invoicingInfoList.addChildElement("ZFKZT").setValue("114");
            invoicingInfoList.addChildElement("ZFKDAT").setValue("115");
            invoicingInfoList.addChildElement("ZFKTIM").setValue("116");

        }
        //合作伙伴开票状态
 /*       SOAPElement poInvoicingInfoList1 = params.addChildElement("poInvoicingInfoList");
        poInvoicingInfoList1.addChildElement("EBELN").setValue("4500692774");
        poInvoicingInfoList1.addChildElement("BELNR").setValue("5105600383");
        poInvoicingInfoList1.addChildElement("GJAHR").setValue("2023");
        poInvoicingInfoList1.addChildElement("BUKRS").setValue("3071");
        poInvoicingInfoList1.addChildElement("ZPOKPZT").setValue("40");
        poInvoicingInfoList1.addChildElement("ZPOKPDAT").setValue("20230616");
        poInvoicingInfoList1.addChildElement("ZPOKPTIM").setValue("112323");
        SOAPElement poInvoicingInfoList = params.addChildElement("poInvoicingInfoList");
        poInvoicingInfoList.addChildElement("EBELN").setValue("4500692774");
        poInvoicingInfoList.addChildElement("BELNR").setValue("");
        poInvoicingInfoList.addChildElement("GJAHR").setValue("");
        poInvoicingInfoList.addChildElement("BUKRS").setValue("3071");
        poInvoicingInfoList.addChildElement("ZPOKPZT").setValue("30");
        poInvoicingInfoList.addChildElement("ZPOKPDAT").setValue("20230616");
        poInvoicingInfoList.addChildElement("ZPOKPTIM").setValue("112323");*/
        System.out.println(soapClient.send(true));
    }
}
